package com.artnology.independence.views.jung;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import com.artnology.independence.Activator;
import com.artnology.independence.model.DVNode;
import com.artnology.independence.model.DVType;
import com.artnology.independence.model.relationships.DVReference;
import com.artnology.independence.reengineering.ReengineeringParser;
import com.artnology.independence.views.DependencyViewer;

import edu.uci.ics.jung.visualization.control.AbstractGraphMousePlugin;

public class DoubleClickMousePlugin extends AbstractGraphMousePlugin implements
		MouseListener {

	private DependencyViewer myViewer = null;

	public DoubleClickMousePlugin(int modifiers, DependencyViewer myViewer) {
		super(modifiers);
		this.myViewer = myViewer;

	}

	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			final Set<DVNode> pickedNodes = new HashSet<DVNode>(myViewer
					.getPickedVertexState().getPicked());

			final Set<ITypeRoot> nodesNeedParsing = new HashSet<ITypeRoot>();
			for (DVNode dvNode : pickedNodes) {
				if (dvNode.isPhantom()) {
					ITypeRoot tr = null;
					try {
						tr = JavaCore
								.create(ResourcesPlugin.getWorkspace()
										.getRoot().getProjects()[0])
								.findType(
										((DVType) dvNode)
												.getFullyQualifiedName())
								.getTypeRoot();
					} catch (JavaModelException e1) {
						e1.printStackTrace();
					}
					nodesNeedParsing.add(tr);
				}
			}
			if (!nodesNeedParsing.isEmpty()) {

				ReengineeringParser.parseTypes(nodesNeedParsing, null);
				

			}
			
			Activator.getDefault().getModel().getMyGraph()
					.refreshGraph(pickedNodes);

		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

}
